package com.bootdo.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.MemberCouponDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.dao.SalesCouponDao;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.MemberCouponDO;
import com.bootdo.wx.domain.SalesCouponDO;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.MemberCouponService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;


@Service
public class MemberCouponServiceImpl implements MemberCouponService {
	@Autowired
	private MemberCouponDao memberCouponDao;
	@Autowired
	private SalesCouponDao salesCouponDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@Override
	public MemberCouponDO get(String id){
		return memberCouponDao.get(id);
	}
	
	@Override
	public List<MemberCouponDO> list(Map<String, Object> map){
		return memberCouponDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberCouponDao.count(map);
	}
	
	@Override
	public int save(MemberCouponDO memberCoupon){
		return memberCouponDao.save(memberCoupon);
	}
	
	@Override
	public int update(MemberCouponDO memberCoupon){
		return memberCouponDao.update(memberCoupon);
	}
	
	@Override
	public int remove(String id){
		return memberCouponDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberCouponDao.batchRemove(ids);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public List<Map<String,Object>> getNewCoupon(Map<String, Object> map) {
		List<Map<String,Object>> newList = new ArrayList<Map<String,Object>>();
		//map.put("hasScore","1");
		//得到现在可以用的优惠券
		List<SalesCouponDO> list = salesCouponDao.getCanList(map);
		String userId = map.get("userId").toString();
		BigDecimal allPrice = new BigDecimal(0);
		for (SalesCouponDO salesCouponDO : list) {
			String couponId = salesCouponDO.getId();
			int limitNumber = salesCouponDO.getLimitNumber();  //优惠券单人最多领取个数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("userId",userId);
			map1.put("couponId", couponId);
			int size = memberCouponDao.list(map1).size();   //自己已经领取个数
			int haveNum = salesCouponDO.getTotalNum() - salesCouponDO.getSendNum();  //优惠券余量
			int canNum = 0;   //自己还可以领取数量
			if (haveNum > limitNumber) {
				canNum = limitNumber-size;
			} else {
				canNum = haveNum-size;
			}
			if (canNum > 0) {
				Map<String,Object> newMap = new HashMap<String,Object>();
				newMap.put("salesId",salesCouponDO.getId());
				newMap.put("name",salesCouponDO.getName());
				newMap.put("goodsId",salesCouponDO.getGoodsId());
				newMap.put("name",salesCouponDO.getName());
				newMap.put("bagsId",salesCouponDO.getBagsId());
				newMap.put("money",salesCouponDO.getMoney());
				newMap.put("enoughMoney",salesCouponDO.getEnoughMoney());
				newMap.put("limit_endAt",salesCouponDO.getLimitEndat());
				newMap.put("limit_starAt",salesCouponDO.getLimitSartat());
				newMap.put("userId",userId);
				newMap.put("couponId",couponId);
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(salesCouponDO.getGoodsId());
				if(goodsGoodsDO==null){
					newMap.put("title","全部商品可用");
				}else{
					newMap.put("title","限商品 "+goodsGoodsDO.getName()+" 可用");
				}
				newList.add(newMap);
			}
		}
		return newList;

	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R getMemberCouponList(Map<String, Object> map) {
		//更新自己过期优惠券
		//Map<String,Object> returnMap=new HashMap<>();
		memberCouponDao.updateCouponByTime(map);
		//PageHelperUtils.page(map);
		String type = map.get("status").toString();
		List<Integer> types = new ArrayList<Integer>();
		if (type.equals("0")) {
			types.add(0);
		} else if (type.equals("1")) {
			types.add(1);
		} else if (type.equals("2")) {
			types.add(2);
			types.add(3);
		}
		map.put("types", types);
		PageHelperUtils.page(map);
		List<Map<String,Object>> list = memberCouponDao.getMyCouponList(map);
		if(list.size()>0){
			for (Map<String, Object> stringObjectMap : list) {
				if (null!=stringObjectMap.get("goodsId") ){
					if ("".equals(stringObjectMap.get("goodsId"))){
						stringObjectMap.put("title","限全部商品可用");
					}else {
						GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(stringObjectMap.get("goodsId").toString());
						if(goodsGoodsDO!=null){stringObjectMap.put("title","限商品 "+goodsGoodsDO.getName()+" 可用");}
					}
				} else{
					stringObjectMap.put("title","限全部商品可用");
				}
			}
		}
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		//returnMap.put("list",list);
		return R.ok().put("data",pageInfo);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public PageInfo<Map<String,Object>> getMyCouponList(Map<String, Object> map) {
		//更新自己过期优惠券
		memberCouponDao.updateCouponByTime(map);
		Integer level = memberUserDao.get(map.get("userId").toString()).getLevel();
		/*if(level>0||level==-2){
			List<Map<String,Object>> list = new ArrayList<>();
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
			pageInfo.setList(list);
			return pageInfo;
		}*/
		PageHelperUtils.page(map);
		String type = map.get("type").toString();
		List<Integer> types = new ArrayList<Integer>();
		if (type.equals("0")) {
			types.add(0);
		} else if (type.equals("1")) {
			types.add(1);
		} else if (type.equals("2")) {
			types.add(2);
			types.add(3);
		}
		map.put("types", types);
		List<Map<String,Object>> list = memberCouponDao.getMyCouponList(map);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R saveCoupon(List<Map<String, Object>> list) {
		for (Map<String, Object> stringObjectMap : list) {
			String userId = stringObjectMap.get("userId").toString();
			String couponId = stringObjectMap.get("couponId").toString();
			Date date = new Date();
			SalesCouponDO salesCouponDO = salesCouponDao.get(couponId);
			//插入会员优惠券表
			MemberCouponDO memberCouponDO = new MemberCouponDO();
			memberCouponDO.setId(UuidUtil.get32UUID());
			memberCouponDO.setDelFlag(0);
			memberCouponDO.setCouponId(couponId);
			memberCouponDO.setType(1);
			memberCouponDO.setCouponName(salesCouponDO.getName());
			memberCouponDO.setCouponMoney(salesCouponDO.getEnoughMoney());
			memberCouponDO.setMoney(salesCouponDO.getMoney());
			memberCouponDO.setStatus(0);
			memberCouponDO.setGoodsId(MapUtil.getStr(stringObjectMap,"goodsId"));
			memberCouponDO.setBagsId(MapUtil.getStr(stringObjectMap,"bagsId"));
			memberCouponDO.setCreateAt(date);
			memberCouponDO.setUpdateAt(date);
			memberCouponDO.setUserId(userId);
			memberCouponDao.save(memberCouponDO);
			//修改优惠券表
			SalesCouponDO couponDO = new SalesCouponDO();
			couponDO.setId(salesCouponDO.getId());
			couponDO.setSendNum(salesCouponDO.getSendNum() + 1);
			salesCouponDao.update(couponDO);
		}

		return R.ok();
	}


	
}
